<template>
  <div id="userLoginPage">
    <h2 class="title">云图库 - 用户注册</h2>
    <a-form
      :model="formState"
      name="basic"
      autocomplete="off"
      @finish="handleSubmit"
    >
      <a-form-item
        name="userAccount"
        :rules="[{ required: true, message: '请输入账号'}]"
      >
        <a-input v-model:value="formState.userAccount" placeholder="请输入账号"/>
      </a-form-item>
      <a-form-item
        name="userPassword"
        :rules="[
          {required: true, message: '请输入密码'},
          {min: 8, message: '密码长度不能小于8位'},
        ]">
        <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码"/>
      </a-form-item>
      <a-form-item
        name="checkPassword"
        :rules="[
          {required: true, message: '请输入密码'},
          {min: 8, message: '密码长度不能小于8位'},
        ]"
      >
        <a-input-password v-model:value="formState.checkPassword" placeholder="请确认密码"/>
      </a-form-item>
      <a-form-item
        name="userEmail"
        :rules="[
          { required: true, message: '请输入qq邮箱'},
          { pattern: /^[a-zA-Z0-9._-]+@qq\.com$/, message: '请输入正确的邮箱，仅支持qq邮箱' },
        ]"
      >
        <a-flex justify="space-between">
          <a-input v-model:value="formState.userEmail" placeholder="请输入邮箱"/>
          <a-button
            type="link"
            :disabled="countdown > 0"
            @click="handleGetCode">
            {{ countdown > 0 ? `${countdown}秒后重试` : '获取验证码' }}
          </a-button>
        </a-flex>
      </a-form-item>
      <a-form-item
        name="code"
        :rules="[{ required: true, message: '请输入验证码'}]"
      >
        <a-input v-model:value="formState.code" placeholder="请输入验证码"/>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">注册</a-button>
      </a-form-item>
      <a-flex justify="space-between">
        <div class="tips1">
          已有账号?
          <RouterLink to="/user/login">去登录</RouterLink>
        </div>
      </a-flex>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { sendEmailUsingPost1, userRegisterUsingPost } from '@/api/userController.ts'
import { useLoginUserStore } from '@/stores/user.ts'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'

const loginUserStore = useLoginUserStore();
const router = useRouter();
const countdown = ref<number>(0);

const formState = reactive<API.UserRegisterRequest> ({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
  code: '',
  userEmail: '',
})

const handleSubmit = async (value:any) => {
  const res = await userRegisterUsingPost(value);
  if (res.data.code === 0 && res.data.data) {
    await loginUserStore.fetchLoginUser();
    message.success('注册成功');
    router.push({
      path: '/user/login',
      replace: true,
    })
  } else {
    message.error('注册失败,' + res.data.message);
  }
}

const handleGetCode = async () => {
  if (countdown.value > 0) return;
  const emailRequest: API.UserEmailRequest = {
    email: formState.userEmail,
    type: 'register',
  }
  try {
    const res = await sendEmailUsingPost1(emailRequest);
    if (res.data.code === 0) {
      message.success('验证码已发送');
    }
  } catch (error) {
    message.error('验证码发送失败，请稍后重试');
    return;
  }
  countdown.value = 60;
  const interval = setInterval(() => {
    countdown.value--;
    if (countdown.value <= 0) {
      clearInterval(interval);
    }
  }, 1000);
}
</script>

<style scoped>
#userLoginPage {
  max-width: 360px;
  margin: 0 auto;
}
.title {
  text-align: center;
  margin-bottom: 20px;
}
.tips1 {
  color: #bbb;
  text-align: left;
}
</style>
